<?php

namespace Helpers;

use Lybc\PhpGB2260\GB2260;

class Area extends GB2260
{

    /**
     * Area constructor.
     *
     * @param $currentCode
     *
     * @throws \Exception
     */
    public function __construct($currentCode = null)
    {
        $this->data = require BASE_PATH . '/vendor/lybc/php-gb2260/data/cn-gb2260.php';
        $this->data = $this->data + [
                110100 => '北京市',
                120100 => '天津市',
                310100 => '上海市',
                500100 => '重庆市',
            ];
        if ($currentCode) {
            if (!array_key_exists($currentCode, $this->data) || is_string($currentCode)) {
                throw new \Exception('invalid code or code is not number');
            }
            $this->currentCode = $currentCode;
        }
    }

    /**
     * 区域code
     * @param $code
     *
     * @return Area|GB2260
     * @throws \Exception
     *
     */
    public static function areaCode($code)
    {
        return new self($code);
    }

    /**
     * 获取所有code
     *
     * @return array|mixed
     *
     * create by qgz 20180731
     */
    public function getAllCodes()
    {
        return $this->data;
    }

    /**
     * 获取所有省份，$withName=true返回id=>name对
     * @param $withName
     * @return array
     * @throws \Lybc\PhpGB2260\InvalidCodeException
     *
     * create by qgz 20180806
     */
    public function getAllProvinces($withName = false)
    {
        $data = [];
        foreach ($this->data as $key => $value) {
            if (substr($key, - 4, 4) === '0000') {
                $this->checkInvalidCode($key);
                if ($withName) {
                    $data[$key] = $value;
                } else {
                    $data[] = $key;
                }
            }
        }

        return $data;
    }

    /**
     * 根据区县code获取市code
     *
     * @param $dist
     * @param $withName
     * @return array|string
     *
     * create by qgz 20180807
     */
    public function getCityByDist($dist, $withName = false)
    {
        if ($withName) {
            if ($dist == 500230) {
                return ['cityId' => 500100, 'cityName' => '重庆市'];
            }
            $city = substr($dist, 0, 4) . '00';

            return ['cityId' => $city, 'cityName' => $this->data[$city]];
        } else {
            if ($dist == 500230) {
                return 500100;
            }

            return substr($dist, 0, 4) . '00';
        }
    }

    /**
     * 根据区县code获取省code
     *
     * @param $dist
     * @param $withName
     * @return array|string
     *
     * create by qgz 20180807
     */
    public function getProvByDist($dist, $withName = false)
    {
        if ($withName) {
            if ($dist == 500230) {
                return ['provId' => 500000, 'provName' => '重庆市'];
            }
            $prov = substr($dist, 0, 2) . '0000';

            return ['provId' => $prov, 'provName' => $this->data[$prov]];
        } else {
            if ($dist == 500230) {
                return 500000;
            }

            return substr($dist, 0, 2) . '0000';
        }
    }

    /**
     * 根据省id获取市数据
     *
     * @param $pro
     * @param bool $withName
     *
     * @return array
     *
     * create by qgz 20190226
     */
    public function getCityByProId($pro, $withName = false)
    {
        $return = [];
        if (!$pro) {
            return $return;
        }
        $strCode = substr($pro, 0, 2);
        if (!$strCode) {
            return $return;
        }
        $allData = self::getAllCodes();
        foreach ($allData as $key => $datum) {
            if (substr($key, 0, 2) == $strCode && substr($key, -1, 2) == '00' && substr($key, 2, 2) != '00') {
                if ($withName) {
                    $return[$key] = $datum;
                } else {
                    $return[] = $key;
                }
            }
        }
        return $return;
    }

    /**
     * 根据市id获取市数据
     *
     * @param $pro
     * @param bool $withName
     *
     * @return array
     *
     * create by qgz 20190226
     */
    public function getCountryByProId($pro, $withName = false)
    {
        $return = [];
        if (!$pro) {
            return $return;
        }
        $strCode = substr($pro, 0, 4);
        if (!$strCode) {
            return $return;
        }
        $allData = self::getAllCodes();
        foreach ($allData as $key => $datum) {
            if (substr($key, 0, 4) == $strCode && substr($key, -1, 2) != '00') {
                if ($withName) {
                    $return[$key] = $datum;
                } else {
                    $return[] = $key;
                }
            }
        }
        return $return;
    }
}
